package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.DianDramaModel;
import com.hangzhou.xyj.ui.listener.RecycleOnItemClickListener;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2016/1/6.
 * 更多名家adapter
 */
public class AlreadyDianAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<DianDramaModel> mData;
    private RecycleOnItemClickListener mItemClickListener;

    public AlreadyDianAdapter(Context context, List<DianDramaModel> data) {
        this.mContext = context;
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData = data;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setOnItemClick(RecycleOnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void update(List<DianDramaModel> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new AlreadyDianViewHolder(mLayoutInflater.inflate(R.layout.layout_already_dian_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof AlreadyDianViewHolder) {
            ((AlreadyDianViewHolder) viewHolder).setData(mData.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (mData == null) return 0;
        return mData.size();
    }

    class AlreadyDianViewHolder extends RecyclerView.ViewHolder {
        MyTextView mTvDramaName;//歌曲名
        MyTextView mTvSingerName;//演唱者
        CircleImageView mCivHeaderIcon;//头像
        MyImageView mIvListen;//试听按钮
        private DianDramaModel mDramaModel;

        public AlreadyDianViewHolder(final View itemView) {
            super(itemView);
            initView(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(itemView, mDramaModel);
                    }
                }
            });
        }

        private void initView(View itemView) {
            mTvDramaName = (MyTextView) itemView.findViewById(R.id.mtv_drama_name);
            mTvSingerName = (MyTextView) itemView.findViewById(R.id.mtv_name);
            mCivHeaderIcon = (CircleImageView) itemView.findViewById(R.id.iv_more_expert_head);
            mIvListen = (MyImageView) itemView.findViewById(R.id.miv_listen);
        }

        public void setData(DianDramaModel model) {
            this.mDramaModel = model;
            mTvDramaName.setText(model.getName());//设置歌曲名
            mTvSingerName.setText(model.getAuthor());//设置演唱者
            mCivHeaderIcon.displayImage(model.getHead());//设置头像
        }
    }
}
